package org.zjsru.order.mapper;

import org.apache.ibatis.annotations.*;
import org.zjsru.order.pojo.Order;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    
    @Insert("INSERT INTO tb_order (user_id, product_id, num, total_amount, status, create_time, update_time) " +
            "VALUES (#{userId}, #{productId}, #{num}, #{totalAmount}, #{status}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Order order);

    @Select("SELECT * FROM tb_order WHERE id = #{id}")
    Order findById(Long id);

    @Select("SELECT * FROM tb_order WHERE status = '未支付' AND create_time < #{timeoutTime}")
    List<Order> findTimeoutOrders(LocalDateTime timeoutTime);

    @Update("UPDATE tb_order SET status = #{status}, update_time = NOW() WHERE id = #{orderId}")
    int updateOrderStatus(@Param("orderId") Long orderId, @Param("status") String status);
}
